package com.learning.batterySwapPro.common.exception;

import com.learning.batterySwapPro.common.response.ResultCode;
import lombok.Getter;
import org.springframework.http.HttpStatus;

/**
 * 业务异常基类
 * @author wangyunyun
 */

@Getter
public class BusinessException extends RuntimeException {
	private final ResultCode errorCode;
	private final HttpStatus httpStatus;
	private final String     customMessage;

	public BusinessException(ResultCode errorCode) {
		super(errorCode.getMessage());
		this.errorCode = errorCode;
		this.httpStatus = resolveHttpStatus(errorCode);
		this.customMessage = null;
	}

	public BusinessException(ResultCode errorCode, String customMessage) {
		super(customMessage);
		this.errorCode = errorCode;
		this.httpStatus = resolveHttpStatus(errorCode);
		this.customMessage = customMessage;
	}

	// 根据错误码映射HTTP状态
	private HttpStatus resolveHttpStatus(ResultCode errorCode) {
		int httpStatus = errorCode.getCode() / 100;
		return HttpStatus.valueOf(httpStatus * 100);
	}
}
